package indi.zjh.dp.db;

import java.lang.reflect.Field;

public class TField {
	public static final int TYPE_INT = 0;
	public static final int TYPE_STRING = 1;
	public static final int TYPE_BIGINT = 2;
	public static final int TYPE_SMALLINT = 3;
	public static final int TYPE_DECIMAL = 4;
	public static final int TYPE_DATETIME = 5;
	public static final int TYPE_TINYINT = 6;
	public static final int TYPE_BLOB = 7;
	public static final int TYPE_TEXT = 8;
	
	// 数据库表的字段名, 会由业务模型的逻辑字段名称进行映射转换
	public String name;
	// 业务模型原来的逻辑名称
	public String orgName;
	public int type;
	public int length;
	public String defaultValue;
	public String comment;
	public boolean isPrimary;
	public Field field;
	
	
	public TField(Field f)
	{
		this.name = "unknown";
		this.type = TYPE_INT;
		this.length = 4;
		this.defaultValue = "unknown";
		this.isPrimary = false;
		f.setAccessible(true);
		this.field = f;
	}
	
	public TField()
	{
		this.name = "unknown";
		this.type = TYPE_INT;
		this.length = 4;
		this.defaultValue = "unknown";
		this.isPrimary = false;
	}
	
	
	public String getString()
	{
		String ret = null;
		switch (this.type) 
		{
		case TYPE_INT:
			ret = "`" + this.name + "` INT(" + this.length + ") NOT NULL";
			break;
		case TYPE_STRING:
			ret = "`" + this.name + "` VARCHAR(" + this.length + ") NOT NULL";
			break;
		case TYPE_BIGINT:
			ret = "`" + this.name + "` BIGINT(" + this.length + ") NOT NULL";
			break;
		case TYPE_SMALLINT:
			ret = "`" + this.name + "` SMALLINT(" + this.length + ") NOT NULL";
			break;
		case TYPE_TINYINT:
			ret = "`" + this.name + "` TINYINT(" + this.length + ") NOT NULL";
			break;
		case TYPE_DECIMAL:
			ret = "`" + this.name + "` DECIMAL(16, " + this.length + ") NOT NULL";
			break;
		case TYPE_DATETIME:
			ret = "`" + this.name + "` DATETIME NOT NULL";
			break;
		case TYPE_BLOB:
			ret = "`" + this.name + "` BLOB NOT NULL";
			break;
		case TYPE_TEXT:
			ret = "`" + this.name + "` TEXT NOT NULL";
			break;
		default:
			break;
		}
		if(this.isPrimary)
		{
			ret += " auto_increment";
			if(!Configuration.isMysql())
			{
				ret = "`" + this.name + "` INTEGER PRIMARY KEY AUTOINCREMENT";
			}
			
		}
		else
		{
			if(this.type == TYPE_INT || this.type == TYPE_BIGINT || this.type == TYPE_SMALLINT || this.type == TYPE_DECIMAL || this.type == TYPE_TINYINT)
			{
				ret += " DEFAULT " + this.defaultValue;
			}
			else if(this.type == TYPE_DATETIME)
			{
				if(this.name.equals("_updated") && Configuration.isMysql())
				{
					
					ret += " DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
				}
				else
					ret += " DEFAULT CURRENT_TIMESTAMP";
			}
			else if(this.type == TYPE_BLOB || this.type == TYPE_TEXT)
			{
				// 不能有默认值
			}
			else
			{
				ret += " DEFAULT '" + this.defaultValue + "'";
			}
			
		}
		
		if(Configuration.isMysql())
		{
			if(this.name.equals("_id"))
				ret += " COMMENT '自增Id'";
			else if(this.name.equals("_f"))
				ret += " COMMENT '数据标记, 0:正常业务数据, 1:测试数据, 2:流水数据, 默认0'";
			else if(this.name.equals("_class"))
				ret += " COMMENT '业务模型类名'";
			else if(this.name.equals("_created"))
				ret += " COMMENT '数据写入时间'";
			else if(this.name.equals("_updated"))
				ret += " COMMENT '数据最后修改时间'";
			else if(this.name.equals("_deleted"))
				ret += " COMMENT '逻辑删除标记 , 0:未删除, 1:已删除, 默认0'";
			else if(this.name.equals("_v"))
				ret += " COMMENT '乐观锁版本号'";
			else
				ret += " COMMENT '" + this.comment + "'";
		}
		
		return ret;
	}

}
